package com.usoft;

import akka.dispatch.ExecutionContexts;
import akka.dispatch.Futures;
import akka.dispatch.Mapper;
import scala.concurrent.Await;
import scala.concurrent.ExecutionContextExecutorService;
import scala.concurrent.Future;
import scala.concurrent.duration.Duration;

import java.util.concurrent.Callable;
import java.util.concurrent.Executors;

/**
 * Created by liyanxin on 2015/1/8.
 */
public class FutureDemo3 {

    public static void main(String args[]) throws Exception {

        final ExecutionContextExecutorService ec = ExecutionContexts.fromExecutorService(
                Executors.newCachedThreadPool());

        Future<String> f1 = Futures.successful("com.hello world");

        Future<Integer> fr = f1.flatMap(new Mapper<String, Future<Integer>>() {
            public Future<Integer> apply(final String s) {
                return Futures.future(new Callable<Integer>() {
                    public Integer call() {
                        return s.length();
                    }
                }, ec);
            }
        }, ec);

        //阻塞在这里，直到Future任务完成
        System.out.println(Await.result(fr, Duration.create(5, "s")));
    }
}
